#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <stdio.h>

void *text = 0x10000;
void *data = 0x20000;
char *file = "mod.bin";

int main() {
  int fd = open(file, O_RDONLY, S_IRUSR | S_IWUSR);
  int (*f)(int, int) = (int (*)(int, int))text;
  int *glob = (int *)data;
  int res;
  
  if (fd == -1) {
    printf ("open error\n");
    return 1;
  }
  
  if (MAP_FAILED == mmap(text, 1024, PROT_EXEC, MAP_SHARED | MAP_FIXED, fd, 0)) {
    printf ("mmap 1 failed\n");
    return 1;
  }
  
  if (MAP_FAILED == mmap(data, 1024, PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED | MAP_FIXED, 0, 0)) {
    printf ("mmap 2 failed\n");
    return 1;
  }
  
  res = f(10, 3);
  
  printf ("res: %d, glob: %d\n", res, *glob);
  
  
    
  munmap(text, 1024);
  munmap(data, 1024);
  
  return 0;
}